<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<title>Ruby on Rails 3.2 Release Notes — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />

<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />

<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />

<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body class="guide">
  <div id="topNav">
    <div class="wrapper">
      <strong class="more-info-label">更多内容 <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
      <span class="red-button more-info-button">
        更多内容
      </span>
      <ul class="more-info-links s-hidden">
        <li class="more-info"><a href="http://weblog.rubyonrails.org/">博客</a></li>
        <li class="more-info"><a href="http://guides.rubyonrails.org/">指南</a></li>
        <li class="more-info"><a href="http://api.rubyonrails.org/">API</a></li>
        <li class="more-info"><a href="http://stackoverflow.com/questions/tagged/ruby-on-rails">提问</a></li>
        <li class="more-info"><a href="https://github.com/rails/rails">到 GitHub 贡献</a></li>
        <li class="more-info"><a href="https://ruby-china.org/">Ruby China 社区</a></li>
      </ul>
    </div>
  </div>
  <div id="header">
    <div class="wrapper clearfix">
      <h1><a href="index.html" title="返回首页">Rails 指南</a></h1>
      <ul class="nav">
        <li><a class="nav-item" href="index.html">首页</a></li>
        <li class="guides-index guides-index-large">
          <a href="index.html" id="guidesMenu" class="guides-index-item nav-item">指南索引</a>
          <div id="guides" class="clearfix" style="display: none;">
            <hr />
              <dl class="L">
                <dt>新手入门</dt>
                <dd><a href="getting_started.html">Rails 入门</a></dd>
                <dt>模型</dt>
                <dd><a href="active_record_basics.html">Active Record 基础</a></dd>
                <dd><a href="active_record_migrations.html">Active Record 迁移</a></dd>
                <dd><a href="active_record_validations.html">Active Record 数据验证</a></dd>
                <dd><a href="active_record_callbacks.html">Active Record 回调</a></dd>
                <dd><a href="association_basics.html">Active Record 关联</a></dd>
                <dd><a href="active_record_querying.html">Active Record 查询接口</a></dd>
                <dt>视图</dt>
                <dd><a href="layouts_and_rendering.html">Rails 布局和视图渲染</a></dd>
                <dd><a href="form_helpers.html">Action View 表单辅助方法</a></dd>
                <dt>控制器</dt>
                <dd><a href="action_controller_overview.html">Action Controller 概览</a></dd>
                <dd><a href="routing.html">Rails 路由全解</a></dd>
              </dl>
              <dl class="R">
                <dt>深入探索</dt>
                <dd><a href="active_support_core_extensions.html">Active Support 核心扩展</a></dd>
                <dd><a href="i18n.html">Rails 国际化 API</a></dd>
                <dd><a href="action_mailer_basics.html">Action Mailer 基础</a></dd>
                <dd><a href="active_job_basics.html">Active Job 基础</a></dd>
                <dd><a href="testing.html">Rails 应用测试指南</a></dd>
                <dd><a href="security.html">Ruby on Rails 安全指南</a></dd>
                <dd><a href="debugging_rails_applications.html">调试 Rails 应用</a></dd>
                <dd><a href="configuring.html">配置 Rails 应用</a></dd>
                <dd><a href="command_line.html">Rails 命令行</a></dd>
                <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
                <dd><a href="working_with_javascript_in_rails.html">在 Rails 中使用 JavaScript</a></dd>
                <dd><a href="autoloading_and_reloading_constants.html">自动加载和重新加载常量</a></dd>
                <dd><a href="caching_with_rails.html">Rails 缓存概览</a></dd>
                <dd><a href="api_app.html">使用 Rails 开发只提供 API 的应用</a></dd>
                <dd><a href="action_cable_overview.html">Action Cable 概览</a></dd>
                <dt>扩展 Rails</dt>
                <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
                <dd><a href="generators.html">创建及定制 Rails 生成器和模板</a></dd>
                <dt>为 Ruby on Rails 做贡献</dt>
                <dd><a href="contributing_to_ruby_on_rails.html">为 Ruby on Rails 做贡献</a></dd>
                <dd><a href="api_documentation_guidelines.html">API 文档指导方针</a></dd>
                <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导方针</a></dd>
                <dt>维护方针</dt>
                <dd><a href="maintenance_policy.html">Ruby on Rails 的维护方针</a></dd>
                <dt>发布记</dt>
                <dd><a href="upgrading_ruby_on_rails.html">Ruby on Rails 升级指南</a></dd>
                <dd><a href="5_0_release_notes.html">Ruby on Rails 5.0 发布记</a></dd>
                <dd><a href="4_2_release_notes.html">Ruby on Rails 4.2 发布记</a></dd>
                <dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 发布记</a></dd>
                <dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 发布记</a></dd>
                <dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 发布记</a></dd>
                <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 发布记</a></dd>
                <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 发布记</a></dd>
                <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 发布记</a></dd>
                <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 发布记</a></dd>
              </dl>
          </div>
        </li>
        <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">贡献</a></li>
        <li><a class="nav-item" href="credits.html">感谢</a></li>
        <li class="guides-index guides-index-small">
          <select class="guides-index-item nav-item">
            <option value="index.html">指南索引</option>
              <optgroup label="新手入门">
                  <option value="getting_started.html">Rails 入门</option>
              </optgroup>
              <optgroup label="模型">
                  <option value="active_record_basics.html">Active Record 基础</option>
                  <option value="active_record_migrations.html">Active Record 迁移</option>
                  <option value="active_record_validations.html">Active Record 数据验证</option>
                  <option value="active_record_callbacks.html">Active Record 回调</option>
                  <option value="association_basics.html">Active Record 关联</option>
                  <option value="active_record_querying.html">Active Record 查询接口</option>
              </optgroup>
              <optgroup label="视图">
                  <option value="layouts_and_rendering.html">Rails 布局和视图渲染</option>
                  <option value="form_helpers.html">Action View 表单辅助方法</option>
              </optgroup>
              <optgroup label="控制器">
                  <option value="action_controller_overview.html">Action Controller 概览</option>
                  <option value="routing.html">Rails 路由全解</option>
              </optgroup>
              <optgroup label="深入探索">
                  <option value="active_support_core_extensions.html">Active Support 核心扩展</option>
                  <option value="i18n.html">Rails 国际化 API</option>
                  <option value="action_mailer_basics.html">Action Mailer 基础</option>
                  <option value="active_job_basics.html">Active Job 基础</option>
                  <option value="testing.html">Rails 应用测试指南</option>
                  <option value="security.html">Ruby on Rails 安全指南</option>
                  <option value="debugging_rails_applications.html">调试 Rails 应用</option>
                  <option value="configuring.html">配置 Rails 应用</option>
                  <option value="command_line.html">Rails 命令行</option>
                  <option value="asset_pipeline.html">Asset Pipeline</option>
                  <option value="working_with_javascript_in_rails.html">在 Rails 中使用 JavaScript</option>
                  <option value="autoloading_and_reloading_constants.html">自动加载和重新加载常量</option>
                  <option value="caching_with_rails.html">Rails 缓存概览</option>
                  <option value="api_app.html">使用 Rails 开发只提供 API 的应用</option>
                  <option value="action_cable_overview.html">Action Cable 概览</option>
              </optgroup>
              <optgroup label="扩展 Rails">
                  <option value="rails_on_rack.html">Rails on Rack</option>
                  <option value="generators.html">创建及定制 Rails 生成器和模板</option>
              </optgroup>
              <optgroup label="为 Ruby on Rails 做贡献">
                  <option value="contributing_to_ruby_on_rails.html">为 Ruby on Rails 做贡献</option>
                  <option value="api_documentation_guidelines.html">API 文档指导方针</option>
                  <option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导方针</option>
              </optgroup>
              <optgroup label="维护方针">
                  <option value="maintenance_policy.html">Ruby on Rails 的维护方针</option>
              </optgroup>
              <optgroup label="发布记">
                  <option value="upgrading_ruby_on_rails.html">Ruby on Rails 升级指南</option>
                  <option value="5_0_release_notes.html">Ruby on Rails 5.0 发布记</option>
                  <option value="4_2_release_notes.html">Ruby on Rails 4.2 发布记</option>
                  <option value="4_1_release_notes.html">Ruby on Rails 4.1 发布记</option>
                  <option value="4_0_release_notes.html">Ruby on Rails 4.0 发布记</option>
                  <option value="3_2_release_notes.html">Ruby on Rails 3.2 发布记</option>
                  <option value="3_1_release_notes.html">Ruby on Rails 3.1 发布记</option>
                  <option value="3_0_release_notes.html">Ruby on Rails 3.0 发布记</option>
                  <option value="2_3_release_notes.html">Ruby on Rails 2.3 发布记</option>
                  <option value="2_2_release_notes.html">Ruby on Rails 2.2 发布记</option>
              </optgroup>
          </select>
        </li>
      </ul>
    </div>
  </div>
  <hr class="hide" />

  <div id="feature">
    <div class="wrapper">
      <h2>Ruby on Rails 3.2 Release Notes</h2><p>Highlights in Rails 3.2:</p>
<ul>
<li>Faster Development Mode</li>
<li>New Routing Engine</li>
<li>Automatic Query Explains</li>
<li>Tagged Logging</li>
</ul>
<p>These release notes cover only the major changes. To learn about various bug
fixes and changes, please refer to the change logs or check out the <a href="https://github.com/rails/rails/commits/3-2-stable">list of
commits</a> in the main Rails
repository on GitHub.</p>

              <div id="subCol">
          <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />目录</h3>
          <ol class="chapters">
<li>
<a href="#upgrading-to-rails-3-2">Upgrading to Rails 3.2</a>

<ul>
<li><a href="#rails-3-2-requires-at-least-ruby-1-8-7">Rails 3.2 requires at least Ruby 1.8.7</a></li>
<li><a href="#what-to-update-in-your-apps">What to update in your apps</a></li>
<li><a href="#what-to-update-in-your-engines">What to update in your engines</a></li>
</ul>
</li>
<li>
<a href="#creating-a-rails-3-2-application">Creating a Rails 3.2 application</a>

<ul>
<li><a href="#vendoring-gems">Vendoring Gems</a></li>
<li><a href="#living-on-the-edge">Living on the Edge</a></li>
</ul>
</li>
<li>
<a href="#major-features">Major Features</a>

<ul>
<li><a href="#faster-development-mode-routing">Faster Development Mode &amp; Routing</a></li>
<li><a href="#automatic-query-explains">Automatic Query Explains</a></li>
<li><a href="#tagged-logging">Tagged Logging</a></li>
</ul>
</li>
<li><a href="#documentation">Documentation</a></li>
<li><a href="#railties">Railties</a></li>
<li><a href="#action-mailer">Action Mailer</a></li>
<li>
<a href="#action-pack">Action Pack</a>

<ul>
<li><a href="#action-controller">Action Controller</a></li>
<li><a href="#action-dispatch">Action Dispatch</a></li>
<li><a href="#action-view">Action View</a></li>
<li><a href="#sprockets">Sprockets</a></li>
</ul>
</li>
<li>
<a href="#active-record">Active Record</a>

<ul>
<li><a href="#active-record-deprecations">Deprecations</a></li>
</ul>
</li>
<li>
<a href="#active-model">Active Model</a>

<ul>
<li><a href="#active-model-deprecations">Deprecations</a></li>
</ul>
</li>
<li><a href="#active-resource">Active Resource</a></li>
<li>
<a href="#active-support">Active Support</a>

<ul>
<li><a href="#deprecations">Deprecations</a></li>
</ul>
</li>
<li><a href="#credits">Credits</a></li>
</ol>

        </div>

    </div>
  </div>

  <div id="container">
    <div class="wrapper">
      <div id="mainCol">
        <h3 id="upgrading-to-rails-3-2">1 Upgrading to Rails 3.2</h3><p>If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3.1 in case you haven't and make sure your application still runs as expected before attempting an update to Rails 3.2. Then take heed of the following changes:</p><h4 id="rails-3-2-requires-at-least-ruby-1-8-7">1.1 Rails 3.2 requires at least Ruby 1.8.7</h4><p>Rails 3.2 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible. Rails 3.2 is also compatible with Ruby 1.9.2.</p><div class="info"><p>Note that Ruby 1.8.7 p248 and p249 have marshalling bugs that crash Rails. Ruby Enterprise Edition has these fixed since the release of 1.8.7-2010.02. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x, jump on to 1.9.2 or 1.9.3 for smooth sailing.</p></div><h4 id="what-to-update-in-your-apps">1.2 What to update in your apps</h4>
<ul>
<li>
<p>Update your Gemfile to depend on</p>
<ul>
<li><code>rails = 3.2.0</code></li>
<li><code>sass-rails ~&gt; 3.2.3</code></li>
<li><code>coffee-rails ~&gt; 3.2.1</code></li>
<li><code>uglifier &gt;= 1.0.3</code></li>
</ul>
</li>
<li><p>Rails 3.2 deprecates <code>vendor/plugins</code> and Rails 4.0 will remove them completely. You can start replacing these plugins by extracting them as gems and adding them in your Gemfile. If you choose not to make them gems, you can move them into, say, <code>lib/my_plugin/*</code> and add an appropriate initializer in <code>config/initializers/my_plugin.rb</code>.</p></li>
<li>
<p>There are a couple of new configuration changes you'd want to add in <code>config/environments/development.rb</code>:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict

# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5

</pre>
</div>
<p>The <code>mass_assignment_sanitizer</code> config also needs to be added in <code>config/environments/test.rb</code>:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict

</pre>
</div>
</li>
</ul>
<h4 id="what-to-update-in-your-engines">1.3 What to update in your engines</h4><p>Replace the code beneath the comment in <code>script/rails</code> with the following content:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
ENGINE_ROOT = File.expand_path('../..', __FILE__)
ENGINE_PATH = File.expand_path('../../lib/your_engine_name/engine', __FILE__)

require 'rails/all'
require 'rails/engine/commands'

</pre>
</div>
<h3 id="creating-a-rails-3-2-application">2 Creating a Rails 3.2 application</h3><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
# You should have the 'rails' RubyGem installed
$ rails new myapp
$ cd myapp

</pre>
</div>
<h4 id="vendoring-gems">2.1 Vendoring Gems</h4><p>Rails now uses a <code>Gemfile</code> in the application root to determine the gems you require for your application to start. This <code>Gemfile</code> is processed by the <a href="https://github.com/carlhuda/bundler">Bundler</a> gem, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.</p><p>More information: <a href="http://bundler.io/">Bundler homepage</a></p><h4 id="living-on-the-edge">2.2 Living on the Edge</h4><p><code>Bundler</code> and <code>Gemfile</code> makes freezing your Rails application easy as pie with the new dedicated <code>bundle</code> command. If you want to bundle straight from the Git repository, you can pass the <code>--edge</code> flag:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails new myapp --edge

</pre>
</div>
<p>If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the <code>--dev</code> flag:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ ruby /path/to/rails/railties/bin/rails new myapp --dev

</pre>
</div>
<h3 id="major-features">3 Major Features</h3><h4 id="faster-development-mode-routing">3.1 Faster Development Mode &amp; Routing</h4><p>Rails 3.2 comes with a development mode that's noticeably faster. Inspired by <a href="https://github.com/paneq/active_reload">Active Reload</a>, Rails reloads classes only when files actually change. The performance gains are dramatic on a larger application. Route recognition also got a bunch faster thanks to the new <a href="https://github.com/rails/journey">Journey</a> engine.</p><h4 id="automatic-query-explains">3.2 Automatic Query Explains</h4><p>Rails 3.2 comes with a nice feature that explains queries generated by Arel by defining an <code>explain</code> method in <code>ActiveRecord::Relation</code>. For example, you can run something like <code>puts Person.active.limit(5).explain</code> and the query Arel produces is explained. This allows to check for the proper indexes and further optimizations.</p><p>Queries that take more than half a second to run are <em>automatically</em> explained in the development mode. This threshold, of course, can be changed.</p><h4 id="tagged-logging">3.3 Tagged Logging</h4><p>When running a multi-user, multi-account application, it's a great help to be able to filter the log by who did what. TaggedLogging in Active Support helps in doing exactly that by stamping log lines with subdomains, request ids, and anything else to aid debugging such applications.</p><h3 id="documentation">4 Documentation</h3><p>From Rails 3.2, the Rails guides are available for the Kindle and free Kindle Reading Apps for the iPad, iPhone, Mac, Android, etc.</p><h3 id="railties">5 Railties</h3>
<ul>
<li><p>Speed up development by only reloading classes if dependencies files changed. This can be turned off by setting <code>config.reload_classes_only_on_change</code> to false.</p></li>
<li><p>New applications get a flag <code>config.active_record.auto_explain_threshold_in_seconds</code> in the environments configuration files. With a value of <code>0.5</code> in <code>development.rb</code> and commented out in <code>production.rb</code>. No mention in <code>test.rb</code>.</p></li>
<li><p>Added <code>config.exceptions_app</code> to set the exceptions application invoked by the <code>ShowException</code> middleware when an exception happens. Defaults to <code>ActionDispatch::PublicExceptions.new(Rails.public_path)</code>.</p></li>
<li><p>Added a <code>DebugExceptions</code> middleware which contains features extracted from <code>ShowExceptions</code> middleware.</p></li>
<li><p>Display mounted engines' routes in <code>rake routes</code>.</p></li>
<li>
<p>Allow to change the loading order of railties with <code>config.railties_order</code> like:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.railties_order = [Blog::Engine, :main_app, :all]

</pre>
</div>
</li>
<li><p>Scaffold returns 204 No Content for API requests without content. This makes scaffold work with jQuery out of the box.</p></li>
<li><p>Update <code>Rails::Rack::Logger</code> middleware to apply any tags set in <code>config.log_tags</code> to <code>ActiveSupport::TaggedLogging</code>. This makes it easy to tag log lines with debug information like subdomain and request id -- both very helpful in debugging multi-user production applications.</p></li>
<li><p>Default options to <code>rails new</code> can be set in <code>~/.railsrc</code>. You can specify extra command-line arguments to be used every time <code>rails new</code> runs in the <code>.railsrc</code> configuration file in your home directory.</p></li>
<li><p>Add an alias <code>d</code> for <code>destroy</code>. This works for engines too.</p></li>
<li><p>Attributes on scaffold and model generators default to string. This allows the following: <code>rails g scaffold Post title body:text author</code></p></li>
<li>
<p>Allow scaffold/model/migration generators to accept "index" and "uniq" modifiers. For example,</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
rails g scaffold Post title:string:index author:uniq price:decimal{7,2}

</pre>
</div>
<p>will create indexes for <code>title</code> and <code>author</code> with the latter being a unique index. Some types such as decimal accept custom options. In the example, <code>price</code> will be a decimal column with precision and scale set to 7 and 2 respectively.</p>
</li>
<li><p>Turn gem has been removed from default Gemfile.</p></li>
<li><p>Remove old plugin generator <code>rails generate plugin</code> in favor of <code>rails plugin new</code> command.</p></li>
<li><p>Remove old <code>config.paths.app.controller</code> API in favor of <code>config.paths["app/controller"]</code>.</p></li>
</ul>
<h5 id="railties-deprecations">5.1 Deprecations</h5>
<ul>
<li>
<code>Rails::Plugin</code> is deprecated and will be removed in Rails 4.0. Instead of adding plugins to <code>vendor/plugins</code> use gems or bundler with path or git dependencies.</li>
</ul>
<h3 id="action-mailer">6 Action Mailer</h3>
<ul>
<li><p>Upgraded <code>mail</code> version to 2.4.0.</p></li>
<li><p>Removed the old Action Mailer API which was deprecated since Rails 3.0.</p></li>
</ul>
<h3 id="action-pack">7 Action Pack</h3><h4 id="action-controller">7.1 Action Controller</h4>
<ul>
<li><p>Make <code>ActiveSupport::Benchmarkable</code> a default module for <code>ActionController::Base,</code> so the <code>#benchmark</code> method is once again available in the controller context like it used to be.</p></li>
<li><p>Added <code>:gzip</code> option to <code>caches_page</code>. The default option can be configured globally using <code>page_cache_compression</code>.</p></li>
<li>
<p>Rails will now use your default layout (such as "layouts/application") when you specify a layout with <code>:only</code> and <code>:except</code> condition, and those conditions fail.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class CarsController
  layout 'single_car', :only =&gt; :show
end

</pre>
</div>
<p>Rails will use <code>layouts/single_car</code> when a request comes in <code>:show</code> action, and use <code>layouts/application</code> (or <code>layouts/cars</code>, if exists) when a request comes in for any other actions.</p>
</li>
<li><p><code>form_for</code> is changed to use <code>#{action}_#{as}</code> as the css class and id if <code>:as</code> option is provided. Earlier versions used <code>#{as}_#{action}</code>.</p></li>
<li><p><code>ActionController::ParamsWrapper</code> on Active Record models now only wrap <code>attr_accessible</code> attributes if they were set. If not, only the attributes returned by the class method <code>attribute_names</code> will be wrapped. This fixes the wrapping of nested attributes by adding them to <code>attr_accessible</code>.</p></li>
<li><p>Log "Filter chain halted as CALLBACKNAME rendered or redirected" every time a before callback halts.</p></li>
<li><p><code>ActionDispatch::ShowExceptions</code> is refactored. The controller is responsible for choosing to show exceptions. It's possible to override <code>show_detailed_exceptions?</code> in controllers to specify which requests should provide debugging information on errors.</p></li>
<li><p>Responders now return 204 No Content for API requests without a response body (as in the new scaffold).</p></li>
<li>
<p><code>ActionController::TestCase</code> cookies is refactored. Assigning cookies for test cases should now use <code>cookies[]</code></p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
cookies[:email] = 'user@example.com'
get :index
assert_equal 'user@example.com', cookies[:email]

</pre>
</div>
<p>To clear the cookies, use <code>clear</code>.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
cookies.clear
get :index
assert_nil cookies[:email]

</pre>
</div>
<p>We now no longer write out HTTP_COOKIE and the cookie jar is persistent between requests so if you need to manipulate the environment for your test you need to do it before the cookie jar is created.</p>
</li>
<li><p><code>send_file</code> now guesses the MIME type from the file extension if <code>:type</code> is not provided.</p></li>
<li><p>MIME type entries for PDF, ZIP and other formats were added.</p></li>
<li><p>Allow <code>fresh_when/stale?</code> to take a record instead of an options hash.</p></li>
<li><p>Changed log level of warning for missing CSRF token from <code>:debug</code> to <code>:warn</code>.</p></li>
<li><p>Assets should use the request protocol by default or default to relative if no request is available.</p></li>
</ul>
<h5 id="action-controller-deprecations">7.1.1 Deprecations</h5>
<ul>
<li>
<p>Deprecated implied layout lookup in controllers whose parent had an explicit layout set:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class ApplicationController
  layout "application"
end

class PostsController &lt; ApplicationController
end

</pre>
</div>
<p>In the example above, <code>PostsController</code> will no longer automatically look up for a posts layout. If you need this functionality you could either remove <code>layout "application"</code> from <code>ApplicationController</code> or explicitly set it to <code>nil</code> in <code>PostsController</code>.</p>
</li>
<li><p>Deprecated <code>ActionController::UnknownAction</code> in favor of <code>AbstractController::ActionNotFound</code>.</p></li>
<li><p>Deprecated <code>ActionController::DoubleRenderError</code> in favor of <code>AbstractController::DoubleRenderError</code>.</p></li>
<li><p>Deprecated <code>method_missing</code> in favor of <code>action_missing</code> for missing actions.</p></li>
<li><p>Deprecated <code>ActionController#rescue_action</code>, <code>ActionController#initialize_template_class</code> and <code>ActionController#assign_shortcuts</code>.</p></li>
</ul>
<h4 id="action-dispatch">7.2 Action Dispatch</h4>
<ul>
<li><p>Add <code>config.action_dispatch.default_charset</code> to configure default charset for <code>ActionDispatch::Response</code>.</p></li>
<li><p>Added <code>ActionDispatch::RequestId</code> middleware that'll make a unique X-Request-Id header available to the response and enables the <code>ActionDispatch::Request#uuid</code> method. This makes it easy to trace requests from end-to-end in the stack and to identify individual requests in mixed logs like Syslog.</p></li>
<li><p>The <code>ShowExceptions</code> middleware now accepts an exceptions application that is responsible to render an exception when the application fails. The application is invoked with a copy of the exception in <code>env["action_dispatch.exception"]</code> and with the <code>PATH_INFO</code> rewritten to the status code.</p></li>
<li><p>Allow rescue responses to be configured through a railtie as in <code>config.action_dispatch.rescue_responses</code>.</p></li>
</ul>
<h5 id="action-dispatch-deprecations">7.2.1 Deprecations</h5>
<ul>
<li>Deprecated the ability to set a default charset at the controller level, use the new <code>config.action_dispatch.default_charset</code> instead.</li>
</ul>
<h4 id="action-view">7.3 Action View</h4>
<ul>
<li>
<p>Add <code>button_tag</code> support to <code>ActionView::Helpers::FormBuilder</code>. This support mimics the default behavior of <code>submit_tag</code>.</p>
<div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
&lt;%= form_for @post do |f| %&gt;
  &lt;%= f.button %&gt;
&lt;% end %&gt;

</pre>
</div>
</li>
<li><p>Date helpers accept a new option <code>:use_two_digit_numbers =&gt; true</code>, that renders select boxes for months and days with a leading zero without changing the respective values. For example, this is useful for displaying ISO 8601-style dates such as '2011-08-01'.</p></li>
<li>
<p>You can provide a namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generated HTML id.</p>
<div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
&lt;%= form_for(@offer, :namespace =&gt; 'namespace') do |f| %&gt;
  &lt;%= f.label :version, 'Version' %&gt;:
  &lt;%= f.text_field :version %&gt;
&lt;% end %&gt;

</pre>
</div>
</li>
<li><p>Limit the number of options for <code>select_year</code> to 1000. Pass <code>:max_years_allowed</code> option to set your own limit.</p></li>
<li>
<p><code>content_tag_for</code> and <code>div_for</code> can now take a collection of records. It will also yield the record as the first argument if you set a receiving argument in your block. So instead of having to do this:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
@items.each do |item|
  content_tag_for(:li, item) do
     Title: &lt;%= item.title %&gt;
  end
end

</pre>
</div>
<p>You can do this:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
content_tag_for(:li, @items) do |item|
  Title: &lt;%= item.title %&gt;
end

</pre>
</div>
</li>
<li><p>Added <code>font_path</code> helper method that computes the path to a font asset in <code>public/fonts</code>.</p></li>
</ul>
<h5 id="action-view-deprecations">7.3.1 Deprecations</h5>
<ul>
<li>Passing formats or handlers to render :template and friends like <code>render :template =&gt; "foo.html.erb"</code> is deprecated. Instead, you can provide :handlers and :formats directly as options: <code>render :template =&gt; "foo", :formats =&gt; [:html, :js], :handlers =&gt; :erb</code>.</li>
</ul>
<h4 id="sprockets">7.4 Sprockets</h4>
<ul>
<li>Adds a configuration option <code>config.assets.logger</code> to control Sprockets logging. Set it to <code>false</code> to turn off logging and to <code>nil</code> to default to <code>Rails.logger</code>.</li>
</ul>
<h3 id="active-record">8 Active Record</h3>
<ul>
<li><p>Boolean columns with 'on' and 'ON' values are type cast to true.</p></li>
<li><p>When the <code>timestamps</code> method creates the <code>created_at</code> and <code>updated_at</code> columns, it makes them non-nullable by default.</p></li>
<li><p>Implemented <code>ActiveRecord::Relation#explain</code>.</p></li>
<li><p>Implements <code>ActiveRecord::Base.silence_auto_explain</code> which allows the user to selectively disable automatic EXPLAINs within a block.</p></li>
<li><p>Implements automatic EXPLAIN logging for slow queries. A new configuration parameter <code>config.active_record.auto_explain_threshold_in_seconds</code> determines what's to be considered a slow query. Setting that to nil disables this feature. Defaults are 0.5 in development mode, and nil in test and production modes. Rails 3.2 supports this feature in SQLite, MySQL (mysql2 adapter), and PostgreSQL.</p></li>
<li>
<p>Added <code>ActiveRecord::Base.store</code> for declaring simple single-column key/value stores.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class User &lt; ActiveRecord::Base
  store :settings, accessors: [ :color, :homepage ]
end

u = User.new(color: 'black', homepage: '37signals.com')
u.color                          # Accessor stored attribute
u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor

</pre>
</div>
</li>
<li>
<p>Added ability to run migrations only for a given scope, which allows to run migrations only from one engine (for example to revert changes from an engine that need to be removed).</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
rake db:migrate SCOPE=blog

</pre>
</div>
</li>
<li><p>Migrations copied from engines are now scoped with engine's name, for example <code>01_create_posts.blog.rb</code>.</p></li>
<li>
<p>Implemented <code>ActiveRecord::Relation#pluck</code> method that returns an array of column values directly from the underlying table. This also works with serialized attributes.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Client.where(:active =&gt; true).pluck(:id)
# SELECT id from clients where active = 1

</pre>
</div>
</li>
<li><p>Generated association methods are created within a separate module to allow overriding and composition. For a class named MyModel, the module is named <code>MyModel::GeneratedFeatureMethods</code>. It is included into the model class immediately after the <code>generated_attributes_methods</code> module defined in Active Model, so association methods override attribute methods of the same name.</p></li>
<li>
<p>Add <code>ActiveRecord::Relation#uniq</code> for generating unique queries.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Client.select('DISTINCT name')

</pre>
</div>
<p>..can be written as:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Client.select(:name).uniq

</pre>
</div>
<p>This also allows you to revert the uniqueness in a relation:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Client.select(:name).uniq.uniq(false)

</pre>
</div>
</li>
<li><p>Support index sort order in SQLite, MySQL and PostgreSQL adapters.</p></li>
<li>
<p>Allow the <code>:class_name</code> option for associations to take a symbol in addition to a string. This is to avoid confusing newbies, and to be consistent with the fact that other options like <code>:foreign_key</code> already allow a symbol or a string.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
has_many :clients, :class_name =&gt; :Client # Note that the symbol need to be capitalized

</pre>
</div>
</li>
<li><p>In development mode, <code>db:drop</code> also drops the test database in order to be symmetric with <code>db:create</code>.</p></li>
<li><p>Case-insensitive uniqueness validation avoids calling LOWER in MySQL when the column already uses a case-insensitive collation.</p></li>
<li><p>Transactional fixtures enlist all active database connections. You can test models on different connections without disabling transactional fixtures.</p></li>
<li>
<p>Add <code>first_or_create</code>, <code>first_or_create!</code>, <code>first_or_initialize</code> methods to Active Record. This is a better approach over the old <code>find_or_create_by</code> dynamic methods because it's clearer which arguments are used to find the record and which are used to create it.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
User.where(:first_name =&gt; "Scarlett").first_or_create!(:last_name =&gt; "Johansson")

</pre>
</div>
</li>
<li>
<p>Added a <code>with_lock</code> method to Active Record objects, which starts a transaction, locks the object (pessimistically) and yields to the block. The method takes one (optional) parameter and passes it to <code>lock!</code>.</p>
<p>This makes it possible to write the following:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Order &lt; ActiveRecord::Base
  def cancel!
    transaction do
      lock!
      # ... cancelling logic
    end
  end
end

</pre>
</div>
<p>as:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Order &lt; ActiveRecord::Base
  def cancel!
    with_lock do
      # ... cancelling logic
    end
  end
end

</pre>
</div>
</li>
</ul>
<h4 id="active-record-deprecations">8.1 Deprecations</h4>
<ul>
<li>
<p>Automatic closure of connections in threads is deprecated. For example the following code is deprecated:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Thread.new { Post.find(1) }.join

</pre>
</div>
<p>It should be changed to close the database connection at the end of the thread:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Thread.new {
  Post.find(1)
  Post.connection.close
}.join

</pre>
</div>
<p>Only people who spawn threads in their application code need to worry about this change.</p>
</li>
<li>
<p>The <code>set_table_name</code>, <code>set_inheritance_column</code>, <code>set_sequence_name</code>, <code>set_primary_key</code>, <code>set_locking_column</code> methods are deprecated. Use an assignment method instead. For example, instead of <code>set_table_name</code>, use <code>self.table_name=</code>.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Project &lt; ActiveRecord::Base
  self.table_name = "project"
end

</pre>
</div>
<p>Or define your own <code>self.table_name</code> method:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Post &lt; ActiveRecord::Base
  def self.table_name
    "special_" + super
  end
end

Post.table_name # =&gt; "special_posts"


</pre>
</div>
</li>
</ul>
<h3 id="active-model">9 Active Model</h3>
<ul>
<li><p>Add <code>ActiveModel::Errors#added?</code> to check if a specific error has been added.</p></li>
<li><p>Add ability to define strict validations with <code>strict =&gt; true</code> that always raises exception when fails.</p></li>
<li><p>Provide mass_assignment_sanitizer as an easy API to replace the sanitizer behavior. Also support both :logger (default) and :strict sanitizer behavior.</p></li>
</ul>
<h4 id="active-model-deprecations">9.1 Deprecations</h4>
<ul>
<li><p>Deprecated <code>define_attr_method</code> in <code>ActiveModel::AttributeMethods</code> because this only existed to support methods like <code>set_table_name</code> in Active Record, which are themselves being deprecated.</p></li>
<li><p>Deprecated <code>Model.model_name.partial_path</code> in favor of <code>model.to_partial_path</code>.</p></li>
</ul>
<h3 id="active-resource">10 Active Resource</h3>
<ul>
<li>Redirect responses: 303 See Other and 307 Temporary Redirect now behave like 301 Moved Permanently and 302 Found.</li>
</ul>
<h3 id="active-support">11 Active Support</h3>
<ul>
<li>
<p>Added <code>ActiveSupport:TaggedLogging</code> that can wrap any standard <code>Logger</code> class to provide tagging capabilities.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))

Logger.tagged("BCX") { Logger.info "Stuff" }
# Logs "[BCX] Stuff"

Logger.tagged("BCX", "Jason") { Logger.info "Stuff" }
# Logs "[BCX] [Jason] Stuff"

Logger.tagged("BCX") { Logger.tagged("Jason") { Logger.info "Stuff" } }
# Logs "[BCX] [Jason] Stuff"

</pre>
</div>
</li>
<li><p>The <code>beginning_of_week</code> method in <code>Date</code>, <code>Time</code> and <code>DateTime</code> accepts an optional argument representing the day in which the week is assumed to start.</p></li>
<li><p><code>ActiveSupport::Notifications.subscribed</code> provides subscriptions to events while a block runs.</p></li>
<li><p>Defined new methods <code>Module#qualified_const_defined?</code>, <code>Module#qualified_const_get</code> and <code>Module#qualified_const_set</code> that are analogous to the corresponding methods in the standard API, but accept qualified constant names.</p></li>
<li><p>Added <code>#deconstantize</code> which complements <code>#demodulize</code> in inflections. This removes the rightmost segment in a qualified constant name.</p></li>
<li><p>Added <code>safe_constantize</code> that constantizes a string but returns <code>nil</code> instead of raising an exception if the constant (or part of it) does not exist.</p></li>
<li><p><code>ActiveSupport::OrderedHash</code> is now marked as extractable when using <code>Array#extract_options!</code>.</p></li>
<li><p>Added <code>Array#prepend</code> as an alias for <code>Array#unshift</code> and <code>Array#append</code> as an alias for <code>Array#&lt;&lt;</code>.</p></li>
<li><p>The definition of a blank string for Ruby 1.9 has been extended to Unicode whitespace. Also, in Ruby 1.8 the ideographic space U`3000 is considered to be whitespace.</p></li>
<li><p>The inflector understands acronyms.</p></li>
<li>
<p>Added <code>Time#all_day</code>, <code>Time#all_week</code>, <code>Time#all_quarter</code> and <code>Time#all_year</code> as a way of generating ranges.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Event.where(:created_at =&gt; Time.now.all_week)
Event.where(:created_at =&gt; Time.now.all_day)

</pre>
</div>
</li>
<li><p>Added <code>instance_accessor: false</code> as an option to <code>Class#cattr_accessor</code> and friends.</p></li>
<li><p><code>ActiveSupport::OrderedHash</code> now has different behavior for <code>#each</code> and <code>#each_pair</code> when given a block accepting its parameters with a splat.</p></li>
<li><p>Added <code>ActiveSupport::Cache::NullStore</code> for use in development and testing.</p></li>
<li><p>Removed <code>ActiveSupport::SecureRandom</code> in favor of <code>SecureRandom</code> from the standard library.</p></li>
</ul>
<h4 id="deprecations">11.1 Deprecations</h4>
<ul>
<li><p><code>ActiveSupport::Base64</code> is deprecated in favor of <code>::Base64</code>.</p></li>
<li><p>Deprecated <code>ActiveSupport::Memoizable</code> in favor of Ruby memoization pattern.</p></li>
<li><p><code>Module#synchronize</code> is deprecated with no replacement. Please use monitor from ruby's standard library.</p></li>
<li><p>Deprecated <code>ActiveSupport::MessageEncryptor#encrypt</code> and <code>ActiveSupport::MessageEncryptor#decrypt</code>.</p></li>
<li><p><code>ActiveSupport::BufferedLogger#silence</code> is deprecated. If you want to squelch logs for a certain block, change the log level for that block.</p></li>
<li><p><code>ActiveSupport::BufferedLogger#open_log</code> is deprecated. This method should not have been public in the first place.</p></li>
<li><p><code>ActiveSupport::BufferedLogger's</code> behavior of automatically creating the directory for your log file is deprecated. Please make sure to create the directory for your log file before instantiating.</p></li>
<li>
<p><code>ActiveSupport::BufferedLogger#auto_flushing</code> is deprecated. Either set the sync level on the underlying file handle like this. Or tune your filesystem. The FS cache is now what controls flushing.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
f = File.open('foo.log', 'w')
f.sync = true
ActiveSupport::BufferedLogger.new f

</pre>
</div>
</li>
<li><p><code>ActiveSupport::BufferedLogger#flush</code> is deprecated. Set sync on your filehandle, or tune your filesystem.</p></li>
</ul>
<h3 id="credits">12 Credits</h3><p>See the <a href="http://contributors.rubyonrails.org/">full list of contributors to Rails</a> for the many people who spent many hours making Rails, the stable and robust framework it is. Kudos to all of them.</p><p>Rails 3.2 Release Notes were compiled by <a href="https://github.com/vijaydev">Vijay Dev</a>.</p>

        <h3>反馈</h3>
        <p>
          我们鼓励您帮助提高本指南的质量。
        </p>
        <p>
          如果看到如何错字或错误，请反馈给我们。
          您可以阅读我们的<a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">文档贡献</a>指南。
        </p>
        <p>
          您还可能会发现内容不完整或不是最新版本。
          请添加缺失文档到 master 分支。请先确认 <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> 是否已经修复。
          关于用语约定，请查看<a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导</a>。
        </p>
        <p>
          无论什么原因，如果你发现了问题但无法修补它，请<a href="https://github.com/rails/rails/issues">创建 issue</a>。
        </p>
        <p>
          最后，欢迎到 <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs 邮件列表</a>参与任何有关 Ruby on Rails 文档的讨论。
        </p>
        <h4>中文翻译反馈</h4>
        <p>贡献：<a href="https://github.com/ruby-china/guides">https://github.com/ruby-china/guides</a>。</p>
      </div>
    </div>
  </div>

  <hr class="hide" />
  <div id="footer">
    <div class="wrapper">
      <p>本著作采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/">创作共用 署名-相同方式共享 4.0 国际</a> 授权</p>
<p>“Rails”，“Ruby on Rails”，以及 Rails Logo 为 David Heinemeier Hansson 的商标。版权所有</p>

    </div>
  </div>

  <script type="text/javascript" src="javascripts/jquery.min.js"></script>
  <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
  <script type="text/javascript" src="javascripts/guides.js"></script>
  <script type="text/javascript" src="javascripts/syntaxhighlighter.js"></script>
  <script type="text/javascript">
    syntaxhighlighterConfig = {
      autoLinks: false,
    };
    $(guidesIndex.bind);
  </script>
</body>
</html>
